/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.deployment.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Artifact;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.Deployment;
import org.eclipse.uml2.uml.DeploymentTarget;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.IGraphEdgeHelper;
import com.cea.papyrus.core.helper.IModelerHelper;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.deployment.Activator;
import com.cea.papyrus.diagram.deployment.preference.DeploymentDiagramPreferenceConstants;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

// TODO: Auto-generated Javadoc
/**
 * Defines management of artifacts.
 * 
 * @author Gilles Cannenterre
 * @since 15 janv. 07
 */
public class NodeArtifactHelper extends NodeHelper {

	/**
	 * The imodeler factory.
	 */
	private IModelerHelper imodelerFactory;
	
	/**
	 * The element factory.
	 */
	private Object elementFactory;
	
	/**
	 * The the deployment.
	 */
	private Deployment theDeployment;
	
	/**
	 * The deployment created.
	 */
	private boolean deploymentCreated;
	
	/**
	 * The channel.
	 */
	private int channel = 203;

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		if(parent instanceof Diagram) {
			return true;
		} else if (parent.getSemanticModel()!=null){
			Element parentElt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();
			if(parentElt!=null){
				return canCreateUMLElement(parentElt);
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecuteCreateElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		// Added by A. Lanusse allow add port to Component
		// Normally Useless since Component is an EncapsulatedClassifier
		if ( 	(umlParent instanceof DeploymentTarget) || 
				(umlParent instanceof Artifact) || 
				(umlParent instanceof Component) ||
				(umlParent instanceof Package)) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#removeElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof DeploymentTarget){			
			// Get the right helper
			imodelerFactory= PapyrusPlugin.getDefault().getFactoryRegistry().getFactory(Deployment.class);
			elementFactory = imodelerFactory.getElementFactory(Deployment.class);	
			
			// Deconnect the deployment
			((IGraphEdgeHelper)elementFactory).deconnect(umlParent, element, theDeployment);					
			
			// Remove the deployment if created
			if (deploymentCreated){			
				((DeploymentTarget)umlParent).getClientDependencies().remove(theDeployment);
			}
			// Remove deployment target from the nearest package
			((DeploymentTarget)umlParent).getNearestPackage().getPackagedElements().remove(element);			
		}
		// Added by A. Lanusse to be used in Component Diagram
		if (umlParent instanceof Component){
			return ((Component)umlParent).getPackagedElements().remove(element);	
		}
		if (umlParent instanceof Artifact){
			return ((Artifact)umlParent).getNestedArtifacts().remove(element);		
		}
		if (umlParent instanceof Package) {				
			return ((Package)umlParent).getPackagedElements().remove(element);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#getBaseString()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public String getBaseString() {
		return "Artifact_"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#setGraphElementPreferences(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	/**
	 * 
	 * 
	 * @param graphElement 
	 */
	@Override
	public void setGraphElementPreferences(GraphElement graphElement) {	
		// Get the preference store
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
		
		// Set colors and font
		graphElement.setBackgroundColor(DeploymentDiagramPreferenceConstants.getPreferenceArtifactBackgroundColor(store));
		graphElement.setBorderColor(DeploymentDiagramPreferenceConstants.getPreferenceArtifactBorderColor(store));
		graphElement.setFontColor(DeploymentDiagramPreferenceConstants.getPreferenceArtifactFontColor(store));
		FontData[] fontdata = DeploymentDiagramPreferenceConstants.getPreferenceArtifactFont(store);
		if (fontdata.length>0){
			graphElement.setFontFamily(fontdata[0].getName());
			graphElement.setFontSize(fontdata[0].getHeight());
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#getNewUMLElementName(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public String getNewUMLElementName(Element umlParent) {
		String name = ""; //$NON-NLS-1$
	
		// i <10000: avoid infinite loops
		for(int i=0; i< 10001; i++ ) {
			name = getBaseString()+i;
			if (umlParent instanceof Package){
				if(null == ((Package)umlParent).getPackagedElement(name)) {
					return name;
				}
			} else if (umlParent instanceof DeploymentTarget){
				if(null == ((DeploymentTarget)umlParent).getNearestPackage().getPackagedElement(name)) {
					return name;
				}
				// Added by A. Lanusse to be used in Component Diagram
			} else if (umlParent instanceof Component){
				if(null == ((Component)umlParent).getPackagedElement(name)) {
					return name;
				}
			} else if (umlParent instanceof Artifact){
				if(null == ((Artifact)umlParent).getNestedArtifact(name)) {
					return name;
				}
			} else {
				if(null == ((Package)umlParent.getNearestPackage()).getPackagedElement(name)) {
					return name;
				}
			}
		}
		return getBaseString()+"X"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public  Element createUMLElement(String name, Element umlParent){
		Debug.debug(this,"createUMLElement(String,Element)", channel); 
		Artifact anArtifact = null;
		if (umlParent instanceof DeploymentTarget) {
			anArtifact = UMLFactory.eINSTANCE.createArtifact();
			anArtifact.setName(name);
			
			// Get the right helper
			imodelerFactory= PapyrusPlugin.getDefault().getFactoryRegistry().getFactory(Deployment.class);
			elementFactory = imodelerFactory.getElementFactory(Deployment.class);	
			
			// No deployment...so let's create one
			if ( ((DeploymentTarget)umlParent).getDeployments().isEmpty() ) {			
				// Create the deployment
				Element element = ((IGraphEdgeHelper)elementFactory).createUMLElement( umlParent);			
				theDeployment = ((Deployment)element);	
				deploymentCreated = true;
			} else {
				// Get the last deployment
				theDeployment = (Deployment)((DeploymentTarget)umlParent).getDeployments().get(((DeploymentTarget)umlParent).getDeployments().size() - 1);				
				deploymentCreated = false;
			}
			
			// Connect the deployment
			((IGraphEdgeHelper)elementFactory).connect(umlParent, anArtifact, theDeployment);			
		
			// Add artifact to the nearest package
			((DeploymentTarget) umlParent).getNearestPackage().getPackagedElements().add(anArtifact);
		} else if (umlParent instanceof Artifact) {
			anArtifact = UMLFactory.eINSTANCE.createArtifact();
			anArtifact.setName(name);
			((Artifact) umlParent).getNestedArtifacts().add(anArtifact);
			//Added by A. Lanusse to add an artifact to a component
		} else if (umlParent instanceof Component) {
			anArtifact = UMLFactory.eINSTANCE.createArtifact();
			anArtifact.setName(name);
			((Component) umlParent).getPackagedElements().add(anArtifact);
		} else if (umlParent instanceof Package) {
			anArtifact = UMLFactory.eINSTANCE.createArtifact();
			anArtifact.setName(name);
			((Package) umlParent).getPackagedElements().add(anArtifact);	
		} else {
			anArtifact = UMLFactory.eINSTANCE.createArtifact();
			anArtifact.setName(name);
			((Package)umlParent.getNearestPackage()).getPackagedElements().add(anArtifact);
		}
		return anArtifact;		
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.helper.NodeHelper#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof DeploymentTarget) {	
			// Get the right helper
			imodelerFactory= PapyrusPlugin.getDefault().getFactoryRegistry().getFactory(Deployment.class);
			elementFactory = imodelerFactory.getElementFactory(Deployment.class);			
			
			// No deployment...so let's create one
			if ( ((DeploymentTarget)umlParent).getDeployments().isEmpty() ) {			
				// Create the deployment
				Element elementCreated = ((IGraphEdgeHelper)elementFactory).createUMLElement( umlParent);			
				theDeployment = ((Deployment)elementCreated);
				deploymentCreated = true;
			} else {
				// Get the last deployment
				theDeployment = (Deployment)((DeploymentTarget)umlParent).getDeployments().get(((DeploymentTarget)umlParent).getDeployments().size() - 1);				
				deploymentCreated = false;
			}
			
			// Connect the deployment
			((IGraphEdgeHelper)elementFactory).connect(umlParent, element, theDeployment);			
		
			// Add artifact to the nearest package
			return ((DeploymentTarget) umlParent).getNearestPackage().getPackagedElements().add((PackageableElement)element);
		}
		if (umlParent instanceof Artifact){
			return ((Artifact)umlParent).getNestedArtifacts().add((Artifact)element);		
		}
		if (umlParent instanceof Component){
			return ((Component)umlParent).getPackagedElements().add((PackageableElement) element);
		}
		if (umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().add((PackageableElement)element);
		}
		return false;
	}

	/**
	 * Gets the graph node size.
	 * 
	 * @return the graph node size
	 */
	public Dimension getGraphNodeSize() {
		return new Dimension(80,100);
	}
}
